﻿using System;
using Dapasoft.Internal.MVP.Presenter;
using Telerik.Web.UI;

namespace Dapasoft.Internal.MVP.View._12.TEMPLATE.CONTROLTEMPLATES
{
    /// <summary>
    /// GridUserControl is a user control, which contains the Rad controls
    /// bound to the Rad ORM.
    /// </summary>
    public partial class GridUserControl : System.Web.UI.UserControl, IScopeView
    {
        #region Page_Load

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.rcbProduct.DataSource = this.oadsNorthwind;
                this.rcbProduct.DataTextField = "CategoryName";
                this.rcbProduct.DataValueField = "CategoryID";
                this.rcbProduct.DataBind();

                BindGrid(1);
            }
        }

        #endregion // Page_Load

        #region Properties

        /// <summary>
        /// Gets or sets the presenter.
        /// </summary>
        /// <value>The presenter.</value>
        public ScopePresenter Presenter { get; set; }

        #endregion // Properties

        #region Methods

        /// <summary>
        /// Binds the grid.
        /// </summary>
        /// <param name="selectedValue">The selected value.</param>
        private void BindGrid(int? selectedValue)
        {
            if (this.Presenter != null)
            {
                this.rgView.DataSource = this.Presenter.GetFilteredData(selectedValue);
                this.rgView.DataBind();
            }
        }

        #endregion // Methods

        #region Events

        /// <summary>
        /// Handles the SelectedIndexChanged event of the rcbProduct control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs"/> instance containing the event data.</param>
        protected void rcbProduct_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if ((sender as RadComboBox).SelectedValue != null)
            {
                int? selectValue =
                  (int?)Int32.Parse((sender as RadComboBox).SelectedValue);

                BindGrid(selectValue);
            }
        }

        #endregion // Events
    }
}